package backend.model.other;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import backend.model.admin.Employee;
import backend.model.base.DBModel;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import shared.other.UserRolesDefinition.UserRole;

@NamedQueries({ @NamedQuery(name = "findUserByUsernameAndPassword", query = "from User u where u.username = :username and u.password = :password"),
                @NamedQuery(name = "findUserByUsername", query = "from User u where u.username = :username"),
                @NamedQuery(name = "findAllActiveUsers", query = "from User u where u.isActive = true")})
@Entity
@Table(name = "user")
public class User extends DBModel {

	@Id
	@GeneratedValue
	@Column(name = "id")
	private int id;

	@Column(name = "username")
	private String username;

	@Column(name = "password")
	private String password;

    @Column(name = "is_active")
    private boolean isActive;

	@Column(name = "role")
	@Enumerated(EnumType.STRING)
	private UserRole userRole;
	
	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "employee_id")
	private Employee employee;

	@Override
    public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

    @Override
    public boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(boolean active) {
        isActive = active;
    }

	public UserRole getRole() {
		return userRole;
	}

	public void setRole(UserRole userRole) {
		this.userRole = userRole;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@Override
    public String getDisplayLabel() {
		return username;
	}

	@Override
    public int compareTo(Object o) {
		if (!(o instanceof User)) {
			return -1;
		} else {
			User otherUser = (User) o;
			return getDisplayLabel().compareTo(otherUser.getDisplayLabel());
		}
	}
}
